package com.screen.screendisplay.service.impl.ne;

import com.screen.screendisplay.entity.ne.NeOpermonRtPv;
import com.screen.screendisplay.mapper.ne.NeOpermonRtPvMapper;
import com.screen.screendisplay.service.ne.NeOpermonRtPvService;
import com.screen.screendisplay.vo.ne.NePvRealPowerVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 光伏实时功率表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOpermonRtPvServiceImpl extends ServiceImpl<NeOpermonRtPvMapper, NeOpermonRtPv> implements NeOpermonRtPvService {

    @Override
    public List<NePvRealPowerVO> getPvRealPower(Integer year) {
        // 构建查询条件
        QueryWrapper<NeOpermonRtPv> queryWrapper = new QueryWrapper<>();
        
        // 如果指定了年份，则根据年份查询
        if (year != null) {
            queryWrapper.eq("year", year);
        }
        
        // 按时间倒序排列，取最新的20条数据
        queryWrapper.orderByDesc("date", "hour", "minute").last("LIMIT 20");
        
        List<NeOpermonRtPv> entities = this.list(queryWrapper);
        
        // 转换为VO对象
        return entities.stream().map(this::convertToVO).collect(Collectors.toList());
    }
    
    /**
     * 实体类转换为VO
     */
    private NePvRealPowerVO convertToVO(NeOpermonRtPv entity) {
        NePvRealPowerVO vo = new NePvRealPowerVO();
        vo.setYear(entity.getYear());
        vo.setMonth(entity.getMonth());
        vo.setDay(entity.getDay());
        vo.setDate(entity.getDate());
        vo.setHour(entity.getHour() != null ? entity.getHour().intValue() : null);
        vo.setMinute(entity.getMinute() != null ? entity.getMinute().intValue() : null);
        vo.setRealtimePower(entity.getRealtimePower());
        vo.setTheoreticalPower(entity.getTheoreticalPower());
        vo.setIrradiance(entity.getIrradiance());
        return vo;
    }

}
